#include "datamodel.h"
#include "storage.h"
#include <QDebug>

DataModel::DataModel(QObject *parent) :
    QAbstractListModel(parent)
{
    m_data = Storage::load(QString("people.txt"));
}

int DataModel::rowCount(const QModelIndex &parent) const
{
    return m_data.size();
}

QVariant DataModel::data(const QModelIndex &index, int role) const
{
    if(role == Qt::DisplayRole && index.isValid())
    {
        return m_data.at(index.row()).getFullName();
    }
    return QVariant();
}


const Person & DataModel::getPerson(const QModelIndex &index) {
    return m_data.at(index.row());
}

void DataModel::saveData() {
    Storage::save(m_data, QString("people.txt"));
}


void DataModel::saveLocal(QModelIndex const & index, QString name, QString lastname,
               QString phone, QString email, QString notes){
    if (index.isValid()) {
        m_data[index.row()].name = name;
        m_data[index.row()].lastname = lastname;
        m_data[index.row()].phone = phone;
        m_data[index.row()].email = email;
        m_data[index.row()].notes = notes;
    }
}
